﻿using System.ComponentModel;
using System.Windows;
using Prism.Common.Interfaces;

namespace Prism
{
  /// <summary>
  /// Interaction logic for Shell.xaml
  /// </summary>
  public partial class Shell : Window
  {
    #region fields

    /// <summary>
    /// Service managing application status value.
    /// </summary>
    private readonly IApplicationManagerService _applicationManagerService;

    #endregion

    #region constructor(s)

    /// <summary>
    /// Creates a new <see cref="Shell"/> instance.
    /// </summary>
    /// <param name="applicationManagerService"></param>
    public Shell(IApplicationManagerService applicationManagerService)
    {
      InitializeComponent();

      _applicationManagerService = applicationManagerService;
      this.Closing += ShellClosing;
    }

    #endregion

    #region methods

    /// <summary>
    /// Called after having called the Close() method of this window,
    /// giving the possibility to cancel closing the window.
    /// </summary>
    /// <param name="sender">Not used.</param>
    /// <param name="cancelEventArgs"></param>
    private void ShellClosing(object sender, CancelEventArgs cancelEventArgs)
    {
      if (_applicationManagerService.ApplicationIsShuttingDown)
        return;

      _applicationManagerService.ApplicationIsShuttingDown = true;
      _applicationManagerService.ShutDownApplicationCommand.Execute(cancelEventArgs);
      _applicationManagerService.ApplicationIsShuttingDown = !cancelEventArgs.Cancel;
    }

    #endregion
  }
}
